package com.autoEcole.service;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import com.autoEcole.dao.EmployeeDAO;
import com.autoEcole.dao.FonctionDAO;
import com.autoEcole.exception.ApplicationException;
import com.autoEcole.exception.DAOException;
import com.autoEcole.model.Employee;
import com.autoEcole.util.Cryptage;

public class EmployeeService {
	
	private EmployeeDAO employeeDAO;
	
	private FonctionDAO fonctionDAO;
	/**
	 * EmployeeService
	 */
	private static EmployeeService employeeService;
	
	/**
	 * Constructor
	 */
	private EmployeeService() {
		super();
	}

	/**
	 * Returns the EmployeeService instance (Design Pattern : Singleton)
	 * 
	 * @return EmployeeService
	 */
	public static synchronized EmployeeService getInstance() {
		if (employeeService == null)
			employeeService = new EmployeeService();
		return employeeService;
	}

	public EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}

	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	public FonctionDAO getFonctionDAO() {
		return fonctionDAO;
	}

	public void setFonctionDAO(FonctionDAO fonctionDAO) {
		this.fonctionDAO = fonctionDAO;
	}

	public boolean authenticate(String username, String password) throws ApplicationException {
		try {
			String mdp = Cryptage.getMD5(password);
			List<Employee> listEmployees = employeeDAO.findAll();
			for (Employee employee : listEmployees) {
				if ( username.equals(employee.getEmployeeNomUtilisateur()) && mdp.equals(employee.getEmployeeMotDePasse()) ) {
					return true;
				}
			}
	        return false;
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}	

	public int create(Employee employee) throws ApplicationException {
		try {
			return employeeDAO.create(employee);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * 
	 */
	public void update(Employee employee) throws ApplicationException {
		try {
			employeeDAO.update(employee);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * retourne l'element si il existe ou null
	 */
	public Employee findById(int id) throws ApplicationException {
		try {
			return employeeDAO.findById(id);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * @throws ParseException 
	 * 
	 */
	public List<Employee> findAll() throws ApplicationException, ParseException {
		try {
			return employeeDAO.findAll();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}
	
	/**
	 * 
	 */
	public Map<Integer , Employee> findAllMap() throws ApplicationException {
		try {
			return employeeDAO.findAllMap();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}
	
	/**
	 * 
	 */	
	public void delete(int employeeId) throws ApplicationException {
		try {
			employeeDAO.delete(employeeId);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}
}
